package com.algrithom.recursive;

import java.util.Stack;

import org.apache.commons.lang.StringUtils;

/**
 * 任意进制转换
 *
 * @author think
 * @version 1.0.0
 * @since 2019/12/22
 */
public class TransformType {
    
    public static void main(String[] args){
        System.out.println(Integer.toString(10,10));
        System.out.println(Integer.parseInt("a",16));
        transform2Other();
        System.out.println(getNum("ZY"));
    }
    
    private static int getResult(int num,int rank){
        return num / rank;
    }
    
    private static int getRemain(int num,int rank){
        return num % rank;
    }
    
    private static void run(int num,int rank,Stack<String> stack){
        int result = getResult(num,rank);
        // 当整除是将余数添加到栈中，退出递归进程
        if (result == 0) {
            stack.push(getRemain(num,rank) + "");
        } else {
            stack.push(getRemain(num,rank) + "");
            run(result,rank,stack);
        }
    }
    
    private static void transform2Other(){
        Stack<String> stack = new Stack<>();
        run(53,2,stack);
        if (!stack.isEmpty()) {
            System.out.println(StringUtils.join(stack,""));
        }
    }
    
    private static Long getNum(String s){
        
        long sum = 0L;
        for (int i = 0; i < s.length(); i++) {
            sum += (long)((s.charAt(i) - 'A' + 1) * Math.pow(26,s.length() - 1 - i));
        }
        return sum;
    }
}
